<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LoginController
 *
 * @author eugene
 */
class LoginController extends RCMS_Core_BaseController
{
	/**
	 * Initialization of the controller
	 *
	 * Here we specify model and other usefull initialization of data
	 */

	public function init() {
		parent::init();
		$this->_model = new Default_Model_LoginModel();
		$this->executeTranslationTablesDump();
	}
	
	public function indexAction() {
		if(isset($this->_session->isLogged) && (true === $this->_session->isLogged)) {
            
			$this->_redirect($this->view->websiteUrl);
		}

		$loginForm = new RCMS_Form_LoginForm();
        $languages = $this->_model->selectAllLanguages();
        ksort($languages);
		$loginForm->getElement('selectlanguage')->setMultiOptions($languages);
		$this->view->loginForm = $loginForm;
		if($this->getRequest()->isPost()) {
			if($this->view->loginForm->isValid($this->getRequest()->getParams())) {

				$login = $this->getRequest()->getParam('login');
				$password = $this->getRequest()->getParam('password');
				
				$userData = $this->_model->selectUserIdByLoginPass($login, $password);
				if(!empty ($userData)) {
					$user = new RCMS_Object_User_User($userData->id);
					$user->setLastLogin(date("Y-m-d H:i:s", time()));
					$user->save();
					$languageId = $this->getRequest()->getParam('selectlanguage');
					$this->_session->translatedLanguageId = $languageId;
					$this->_session->langId = $languageId;
					$this->_session->currentUser = serialize($user);
					$this->_session->isLogged = true;
					$this->_cache->clean('all');
					$this->_redirect($this->view->websiteUrl);
				}
				else {
					$this->view->invalidLogin = true;
					$this->view->invalidLoginPas = true;
				}
			}
			else {
				$this->view->invalidLogin = true;
				$this->view->errorElements = $this->view->loginForm->getMessages();
			}
		}
	}

	public function logoutAction() {
		if(isset($this->_session->isLogged) && (true === $this->_session->isLogged)) {
			$this->_cache->clean('all');
			$this->_session->unsetAll();
		}
		$this->_redirect($this->view->websiteUrl);
	}


	public function executeTranslationTablesDump()
	{
		$lastDumpTime = $this->_model->selectConfigValue('last_dump_time');
		$currentDate = date("Y-m-d");
		if($currentDate!=$lastDumpTime){
			$this->executeTablesDump($currentDate);
			$this->updateLastDumpTime($currentDate);
		}
	}

	public function executeTablesDump($currentDate){
		$path = unserialize(Zend_Registry::get('config'))->website->website->path;
		//$this->_model->selectTranslationTablesDataInFile($currentDate,$path);
		$string = "";
		$lexem =  $this->_model->selectTableData($currentDate,$path,'lexem');
		foreach ($lexem as $item){
			$string.=$item['id']."~$~".$item['original_value']."\n";
		}
		$this->saveDumpFile($currentDate, $path, 'lexem', $string);

		$string="";
		$lexemHasLanguage =  $this->_model->selectTableData($currentDate,$path,'lexem_has_language');
		foreach ($lexemHasLanguage as $item){
			$string.=$item['lexem_id']."~$~".$item['language_id']."~$~".$item['value']."\n";
		}
		$this->saveDumpFile($currentDate, $path, 'lexem_has_language', $string);


		$string="";
		$translateLanguages =  $this->_model->selectTableData($currentDate,$path,'translate_languages');

		foreach ($translateLanguages as $item){
			$string.=$item['id']."~$~".$item['language']."\n";
		}
		$this->saveDumpFile($currentDate, $path, 'translate_languages', $string);
	}

	public function updateLastDumpTime($currentDate){
		$this->_model->updateConfigVariableByName('last_dump_time',$currentDate);
	}

	public function saveDumpFile($currentDate,$path,$tablename,$data){
		if(!is_file($path."public/translationdump/".$tablename."-".$currentDate.".txt")) {
			file_put_contents($path."public/translationdump/".$tablename."-".$currentDate.".txt", $data);
		}
	}
}

